import com.google.appengine.api.users.User;
import com.googlecode.objectify.helper.DAOBase
import com.googlecode.objectify.ObjectifyService
import scriptlerweb.Script
import scriptlerweb.Provider
import groovyx.gaelyk.logging.GroovyLogger


/*
 * File Name: objectifyplugindescriptor.groovy
 *
 * Created By: Jeff Schwartz
 * 
 * Date Created: 20100715
 * 
 * Change log:
 * 
 * Remarks:
 *  
 * This script should be placed in the war/WEB-INF/plugins folder.
 * 
 * This script adds the variable 'dao' to gaelyk's binding for templates and groovlets.
 * 
 * The 'dao' variable is a reference to an instance of 'DAO' (data access object) which
 * is derived from com.googlecode.objectify.helper.DAOBase and supports
 * the registering of the POJOs you will use with the objectify service as well
 * as providing easy access to the objectify service itself through 'dao.ofy()'.
 * 
 * Therefore, when your appengine application is loaded your code in your templates and
 * groovlets will have full access to objectify just by referencing 'dao.ofy()'.
 * 
 */

/*
 * DAO - a simple data access object modeled after 
 * the one in objectify's documentation.
 * 
 * Usage: Register your pojos and name your binding variable which 
 * I have defaulted to dao. Feel free to rename this to anything you
 * want. Just change 'dao' below to your preferred variable name.
 * 
 */

public class DAO extends DAOBase
{
	final String ID_PREFIX = "scriptlerweb."
	def log = new GroovyLogger("scriptlerweb.DAO")
	
	// Resgister your POJOs here
	static {
		ObjectifyService.register(Script.class); // replace with your own pojos
		ObjectifyService.register(Provider.class); 
	}
	
	public Script loadScript(Object id) {
		if(id.startsWith(ID_PREFIX)){
			id = id.substring(ID_PREFIX.length());
		}
		return ofy().find(Script.class, Long.valueOf(id));
	}
	
	public Provider loadProvider(Object id) {
		return ofy().find(Provider.class, Long.valueOf(id));
	}
	
	public Provider loadProviderByAccountUserId(String accountUserId){
		return ofy().query(Provider.class).filter("accountUserId", accountUserId).get();
	}

	public Provider loadProviderByName(String name){
		return ofy().query(Provider.class).filter("name", name).get();
	}
	
	public List<Script> loadScriptsByProviderId(Object id){
		if(id.startsWith(ID_PREFIX)){
			id = id.substring(ID_PREFIX.length());
		}
		return ofy().query(Script.class).filter("providerId", Long.valueOf(id)).order("name").list();
	}
	
	public Provider loadCurrentProvider (User user) {
		def provider
		if(user){
			log.fine "OpenIdUser: ${user.getUserId()}"
			provider = loadProviderByAccountUserId(user.getUserId())
	
			log.fine "loaded provider? found: $provider"
			if(!provider){
				provider = new Provider(accountUserId: user.getUserId())
				ofy().put(provider)
				log.fine "saved provider: $provider"
			}
		}
		
		return provider
	}
}



/*
 * Add variable dao in the binding.
 * Feel free to rename this variable to anything you want.
 * 
 */
binding {
	// an instance of class DAO from objectify library
	dao = new DAO()
}
